package com.situ.ftbserver.api;

import com.github.pagehelper.PageInfo;
import com.situ.ftbserver.mapper.CourseMapper;
import com.situ.ftbserver.model.Course;
import com.situ.ftbserver.model.Reserve;
import com.situ.ftbserver.model.search.ReserveSearchBean;
import com.situ.ftbserver.service.ReserveService;
import com.situ.ftbserver.util.JsonResult;
import com.situ.ftbserver.util.PaginateInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/api/v1/reserve",produces = MediaType.APPLICATION_JSON_VALUE)
public class ReserveApi {
    private ReserveService reserveService;
    private CourseMapper courseMapper;
    @Autowired
    public void setReserveService(ReserveService reserveService) {
        this.reserveService = reserveService;
    }

    @Autowired
    public void setCourseMapper(CourseMapper courseMapper) {
        this.courseMapper = courseMapper;
    }

    @GetMapping
    public ResponseEntity<JsonResult<?>> findAll(@RequestParam(defaultValue ="1")  Integer pageNo,
                                                 @RequestParam(defaultValue ="10") Integer pageSize,
                                                 ReserveSearchBean rsb) {
//        System.out.println(pageNo+"anannanananannanananan"+pageSize);
        PaginateInfo pi = PaginateInfo.of(pageNo, pageSize);
        List<Reserve> reserves = reserveService.findAll(pi, rsb);
        PageInfo<Reserve> pageInfo = new PageInfo<>(reserves);
        return ResponseEntity.ok(JsonResult.success(pageInfo));
    }
    @GetMapping("/getCourseReserveCount")
    public ResponseEntity<JsonResult<?>> getCourseReserveCount() {
        List<Map<String, Integer>> courseReserveCount = reserveService.getCourseReserveCount();
        return ResponseEntity.ok(JsonResult.success(courseReserveCount));
    }
    @DeleteMapping
    public ResponseEntity<JsonResult<?>> delete(@RequestBody Integer[] ids) {
        int count = reserveService.delete(ids);
        return ResponseEntity.ok(JsonResult.success(count));
    }
    @PostMapping
    public ResponseEntity<JsonResult<?>> save(@RequestBody Reserve reserve) {
        int courseId = reserve.getCourseId();
        Course course = courseMapper.selectById(courseId);
        String status = course.getStatus();
        if("已满员".equals( status) || "已取消".equals( status)){
            return ResponseEntity.ok(JsonResult.fail("课程已满员或已取消"));
        }
        Reserve byCourseAndMember = reserveService.findByCourseAndMember(reserve.getCourseId(), reserve.getMemberId());
        if(byCourseAndMember != null){
            return ResponseEntity.ok(JsonResult.fail("该会员已选择该课程"));
        }
        reserve.setTime(LocalDateTime.now());
        boolean result = reserveService.save(reserve);
        if (result) {
            return ResponseEntity.ok(JsonResult.success(reserve));
        } else {
            return ResponseEntity.ok(JsonResult.fail("保存失败"));
        }
    }
    @PutMapping
    public ResponseEntity<JsonResult<?>> update(@RequestBody Reserve reserve) {
        reserve.setTime(LocalDateTime.now());

        int id = reserve.getId();
        Reserve oldReserve = reserveService.findById(id);
        Course  oldCourse = courseMapper.selectById(oldReserve.getCourseId());
        Course newCourse = courseMapper.selectById(reserve.getCourseId());
        if(oldCourse.equals(newCourse)){//课程没变
            return ResponseEntity.ok(JsonResult.success(reserve));
        }else{
            if("已满员".equals(newCourse.getStatus()) || "已取消".equals(newCourse.getStatus())){
                return ResponseEntity.ok(JsonResult.fail("所选课程已满员或已取消"));
            }
        }
        boolean result = reserveService.update(reserve);
        if (result) {
            return ResponseEntity.ok(JsonResult.success(reserve));
        } else {
            return ResponseEntity.ok(JsonResult.fail("修改失败"));
        }
    }
}
